-- Particle
-- 粒子
Particle = class()

function Particle:init(x, y)
    self.x = x
    self.y = y
    self.particle = {}
    self.maxparticle = 20000
    self.particlecount = 0
end

function Particle:emit()
    if self.particlecount < self.maxparticle then
        local dir = vec2(2, 0):rotate(math.random() - 0.5) * math.random(2, 4)
        local size = math.random(4, 6)
        local life = math.random(20, 30)
        local bubble = {pos = vec2(self.x, self.y), 
                        posO = vec2(self.x, self.y), 
                        dir = dir, 
                        size = size, 
                        life = life}
        table.insert(self.particle, bubble)
        self.particlecount = self.particlecount + 1
    end
end

function Particle:update()
    for k,v in pairs(self.particle) do
        v.posO = v.pos
        v.pos = v.pos + v.dir  
        v.life = v.life - 1
        if v.life == 0 then
            self.particle[k] = nil
            self.particlecount = self.particlecount - 1
        end
    end
end

function Particle:draw(a)
    pushStyle()

    ellipseMode(CENTER)
    stroke(255)
    strokeWidth(4)
    fill(204, 211, 213, 100)
        
    for k,v in pairs(self.particle) do
        ellipse(v.pos.x + (v.pos.x - v.posO.x) * a, v.pos.y + (v.pos.y - v.posO.y) * a, v.size)
    end
    
    popStyle()
end
